<!--  -->
<template>
	<div>
		<cu-custom :needBack="true" bgColor="bgNormal" topTitle="指标列表" titleClass="commTitleWhite"
			backIconColor="#fff"></cu-custom>
		<view :style="{ height: getHeight() }" class="content">
			<tki-tree ref="tkitree" :range="range" rangeKey="target" :defaultIcon="img1" :currentIcon="img2"
				@confirm="goList" :isScore="true" :isUpdate="true"/>
		</view>
	</div>
</template>

<script>
	import {
		getTargetManagement
	} from "@/api/report.js";
	import {
		getIsHave,
		isUserCheck,
		currentUserTree
	} from "@/api/expert.js";
	import tkiTree from "@/components/sy-tree-number/tki-tree.vue";

	export default {
		data() {
			return {
				img1: require("@/static/images/xiala1.png"),
				img2: require("@/static/images/xiala2.png"),
				range: [],
				userInfo: uni.getStorageSync("userInfo"),
				currentReportId: uni.getStorageSync("currentReportId"),
			};
		},
		onShow() {
			this.userInfo = uni.getStorageSync("userInfo")
			this.getList();
		},
		methods: {
			getIdsById(id, tree) {
				const node = this.findNodeById(id, tree); // 根据id查找对应的节点
				if (node) {
					return this.getAllIds(node); // 返回当前节点下的所有子节点id集合
				}
				return [];
			},

			findNodeById(id, tree) {
				for (const node of tree) {
					// 遍历数组中的每一个节点
					if (node.id === id) {
						return node; // 如果当前节点的id与指定id匹配，则返回当前节点
					}
					if (node.children) {
						// 如果当前节点有子节点
						const result = this.findNodeById(id, node.children); // 递归查找子节点
						if (result) {
							return result; // 如果找到了匹配的节点，则返回该节点
						}
					}
				}
				return null; // 如果未找到匹配的节点，则返回null
			},

			getAllIds(node) {
				const ids = [node.id]; // 将当前节点的id添加到集合中
				if (node.children) {
					// 如果当前节点有子节点
					for (const child of node.children) {
						// 遍历所有子节点
						ids.push(...this.getAllIds(child)); // 递归获取子节点的id并添加到集合中
					}
				}
				return ids;
			},

			getHeight() {
				return `calc(100vh - ${this.CustomBar}px)`;
			},
			async goList(item) {
				let ids = this.getIdsById(item.id, this.range);
				let data = {
					declarationInfoId: this.currentReportId,
					targetManagementId: item.id,
				};
				//判断当前指标知否已经填报
				let res = await getIsHave(data);
				console.log(res);
				if (res.data) {
					if (this.userInfo.roles[0].roleKey == "EXPERT") {
						uni.navigateTo({
							url: `/packageB/applicationMaterials/write?id=${item.id}&targetDeclarationRecordId=${res.data.id}&auditStatus=${res.data.auditStatus}`,
						});
					} else {
						uni.navigateTo({
							url: `/packageB/applicationMaterials/writeZhiDao?id=${item.id}&targetDeclarationRecordId=${res.data.id}&score=${res.data.score}`,
						});
					}
				} else {
					uni.showToast({
						title: "当前指标未填报",
						icon: "none",
						duration: 2000,
					});
				}
				
			},

			async getList() {
				// let res = await getTargetManagement({
				// 	socialOrganizationTypeId: uni.getStorageSync(
				// 		"currentSocialOrganizationTypeId"
				// 	),
				// });
				
				let res = await currentUserTree({
					declarationInfoId: uni.getStorageSync(
						"currentReportId"
					),
					target: ''
				});
				let tree = res.data;
				this.changeKey(tree);
				this.range = tree;
			},

			changeKey(arr) {
				for (let i = 0; i < arr.length; i++) {
					if (arr[i].childs && arr[i].childs.length > 0) {
						arr[i].children = arr[i].childs;
						this.changeKey(arr[i].childs);
					}
				}
			},
		},
		components: {
			tkiTree,
		},
	};
</script>
<style scoped>
	.content {
		overflow-y: scroll;
	}
</style>